package com.ctshk.rpc.scenic.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.Channel;
import com.ctshk.common.enums.ChannelType;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.scenic.entity.ScenicInitializationSettings;
import com.ctshk.rpc.scenic.entity.ScenicInitializationSettingsChannel;
import com.ctshk.rpc.scenic.mapper.ScenicInitializationSettingsChannelMapper;
import com.ctshk.rpc.scenic.mapper.ScenicInitializationSettingsMapper;
import com.ctshk.rpc.scenic.req.ScenicSupplierApiSettingReq;
import com.ctshk.rpc.scenic.service.IScenicInitializationSettingsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 门票api初始化设置表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-02-25
 */
@DubboService
public class ScenicInitializationSettingsServiceImpl extends ServiceImpl<ScenicInitializationSettingsMapper, ScenicInitializationSettings> implements IScenicInitializationSettingsService {
    @Autowired
    ScenicInitializationSettingsMapper scenicInitializationSettingsMapper;
    @Autowired
    ScenicInitializationSettingsChannelMapper scenicInitializationSettingsChannelMapper;

    @Override
    @Transactional
    public Result apiSetUp(ScenicSupplierApiSettingReq settingReq, TokenUser currentUser){
        QueryWrapper<ScenicInitializationSettings> settingsQueryWrapper = new QueryWrapper<>();
        settingsQueryWrapper.eq("supplier_id",settingReq.getSupplierId());
        ScenicInitializationSettings scenicInitializationSettingsOld = scenicInitializationSettingsMapper.selectOne(settingsQueryWrapper);
        if(scenicInitializationSettingsOld != null){
            Long settingId = scenicInitializationSettingsOld.getId();
            scenicInitializationSettingsMapper.deleteById(settingId);
            QueryWrapper<ScenicInitializationSettingsChannel> queryWrapper = new QueryWrapper();
            queryWrapper.eq("setting_id",settingId);
            List<ScenicInitializationSettingsChannel> list = scenicInitializationSettingsChannelMapper.selectList(queryWrapper);
            if(list != null && list.size() >=0){
                List<Long> idS = new ArrayList<>();
                for(ScenicInitializationSettingsChannel settingsChannel : list){
                    idS.add(settingsChannel.getId());
                }
                scenicInitializationSettingsChannelMapper.deleteBatchIds(idS);
            }
        }
        ScenicInitializationSettings scenicInitializationSettings = new ScenicInitializationSettings();
        Long id = SnowflakeIdWorker.nextId();
        scenicInitializationSettings.setPackStatus(settingReq.getAllowPackage());
        scenicInitializationSettings.setSupplierId(Long.parseLong(settingReq.getSupplierId()));
        scenicInitializationSettings.setSupplierName(settingReq.getSupplierName());
        scenicInitializationSettings.setCreateId(currentUser.getId());
        scenicInitializationSettings.setGmtCreate(LocalDateTime.now());
        scenicInitializationSettings.setServiceChargeType(settingReq.getReserveChargeType());
        scenicInitializationSettings.setServiceCharge(settingReq.getReserveCharge());
        scenicInitializationSettings.setRefundServiceChargeType(settingReq.getCtsChargeType());
        scenicInitializationSettings.setRefundServiceCharge(settingReq.getCtsCharge());
        scenicInitializationSettings.setId(id);
        scenicInitializationSettings.setAmplitudeType(settingReq.getAdjustType());
        scenicInitializationSettings.setAmplitudePrice(settingReq.getAmount());
        scenicInitializationSettingsMapper.insert(scenicInitializationSettings);
        List<ScenicInitializationSettingsChannel> list = new ArrayList<>();
        //线上渠道
        List<String> onlineChannelType = settingReq.getOnlineChannelType();
        if (onlineChannelType != null && onlineChannelType.size() >= 0) {
            for(String type : onlineChannelType){
                ScenicInitializationSettingsChannel scenicInitializationSettingsChannel = new ScenicInitializationSettingsChannel();
                scenicInitializationSettingsChannel.setSettingId(id);
                scenicInitializationSettingsChannel.setChannelType(ChannelType.ONLINE_CHANNEL_TYPE.getCode());
                scenicInitializationSettingsChannel.setChannelId(Long.parseLong(type));
                scenicInitializationSettingsChannel.setGmtCreate(LocalDateTime.now());
                if("3".equals(type)){
                    scenicInitializationSettingsChannel.setChannelName(Channel.THIRD_PLATFORM.getMsg());
                }else if("4".equals(type)){
                    scenicInitializationSettingsChannel.setChannelName(Channel.PLATFORM.getMsg());
                }
                list.add(scenicInitializationSettingsChannel);
            }
        }
        //线下渠道
        List<String> offlineChannelType = settingReq.getOfflineChannelType();
        if(offlineChannelType != null && offlineChannelType.size() >=1){
            for(String type : offlineChannelType){
                ScenicInitializationSettingsChannel scenicInitializationSettingsChannel = new ScenicInitializationSettingsChannel();
                scenicInitializationSettingsChannel.setSettingId(id);
                scenicInitializationSettingsChannel.setGmtCreate(LocalDateTime.now());
                scenicInitializationSettingsChannel.setChannelType(ChannelType.OFFLINE_CHANNEL_TYPE.getCode());
                scenicInitializationSettingsChannel.setChannelId(Long.parseLong(type));
                if("1".equals(type)){
                    scenicInitializationSettingsChannel.setChannelName(Channel.AUTARKY.getMsg());
                }else if("2".equals(type)){
                    scenicInitializationSettingsChannel.setChannelName(Channel.AGENT.getMsg());
                }
                list.add(scenicInitializationSettingsChannel);
            }
        }
        if(list != null && list.size() >=0){
            scenicInitializationSettingsChannelMapper.insertBatchSomeColumn(list);
        }
        return Result.success(id);
    }

}
